/*
 * @Author: your name
 * @Date: 2021-12-20 10:19:57
 * @LastEditTime: 2021-12-22 15:00:28
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \first_one_file\react-travel\src\redux\languageRedux.ts
 */
import i18n from 'i18next';
import { CHANGE_LANGUAGE, ADD_LANGUAGE } from './languageActions'


export interface LanuageState {
  language: "en" | "zh",
  languageList: { name: string, code: string }[]
}

const defaultState: LanuageState = {
  language: 'zh',
  languageList: [
    { name: "中文", code: "zh" },
    { name: "English", code: "en" },
  ]
}

export default (state = defaultState, action: any) => {
  // console.log('state, action :>> ', state, action);
  // console.log('{ ...state, language: action.payload } :>> ', { ...state, language: action.payload });
  switch (action.type) {
    case CHANGE_LANGUAGE:
      // 这样处理是不标准的，有副作用
      i18n.changeLanguage(action.payload);
      return { ...state, language: action.payload };
    case ADD_LANGUAGE:
      return {
        ...state,
        languageList: [...state.languageList, action.payload],
      };
    default:
      return state;
  }
}
